//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BRLCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency () : CCurrency(CBRLCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phBRLCurrency = NULL;
#endif
        _ppBRLCurrency = new boost::shared_ptr<QuantLib::BRLCurrency> (new QuantLib::BRLCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (boost::shared_ptr<QuantLib::BRLCurrency>& childNative, Object^ owner) : CCurrency(CBRLCurrency::typeid)
{
#ifdef HANDLE
	_phBRLCurrency = NULL;
#endif
	_ppBRLCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
}
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (QuantLib::BRLCurrency& childNative, Object^ owner) : CCurrency(CBRLCurrency::typeid)
{
#ifdef HANDLE
	_phBRLCurrency = NULL;
#endif
	_ppBRLCurrency = new boost::shared_ptr<QuantLib::BRLCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
    _BRLCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (CBRLCurrency^ copy) : CCurrency(CBRLCurrency::typeid)
{
#ifdef HANDLE
	_phBRLCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBRLCurrency = new boost::shared_ptr<QuantLib::BRLCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
    }
}
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (System::Type^ t) : CCurrency(CBRLCurrency::typeid)
{
#ifdef HANDLE
	_phBRLCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CBRLCurrency::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (QuantLib::Handle<QuantLib::BRLCurrency>& childNative, Object^ owner)  : CCurrency(CBRLCurrency::typeid)
{
	_phBRLCurrency = &childNative;
	_ppBRLCurrency = &static_cast<boost::shared_ptr<QuantLib::BRLCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
    _BRLCurrencyOwner = owner;
}
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (QuantLib::Handle<QuantLib::BRLCurrency> childNative)  : CCurrency(CBRLCurrency::typeid)
{
	_phBRLCurrency = &childNative;
	_ppBRLCurrency = &static_cast<boost::shared_ptr<QuantLib::BRLCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CBRLCurrency::CBRLCurrency (QuantLib::BRLCurrency childNative)  : CCurrency(CBRLCurrency::typeid)
{
#ifdef HANDLE
	_phBRLCurrency = NULL;
#endif
	_ppBRLCurrency = new boost::shared_ptr<QuantLib::BRLCurrency> (new QuantLib::BRLCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBRLCurrency));
}
#endif

Cephei::QL::Currencies::CBRLCurrency::~CBRLCurrency ()
{
    if (_ppBRLCurrency != NULL)
    {
	    delete _ppBRLCurrency;
        _ppBRLCurrency = NULL;
    }
}
Cephei::QL::Currencies::CBRLCurrency::!CBRLCurrency ()
{
    if (_ppBRLCurrency != NULL)
    {
	    delete _ppBRLCurrency;
    }
}
QuantLib::BRLCurrency& Cephei::QL::Currencies::CBRLCurrency::GetReference ()
{
    if (_ppBRLCurrency == NULL) throw gcnew NativeNullException ();
	return **_ppBRLCurrency;
}
boost::shared_ptr<QuantLib::BRLCurrency>& Cephei::QL::Currencies::CBRLCurrency::GetShared ()
{
    if (_ppBRLCurrency == NULL) throw gcnew NativeNullException ();
	return *_ppBRLCurrency;
}
QuantLib::BRLCurrency* Cephei::QL::Currencies::CBRLCurrency::GetPointer ()
{
    if (_ppBRLCurrency == NULL) throw gcnew NativeNullException ();
	return &**_ppBRLCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BRLCurrency>& Cephei::QL::Currencies::CBRLCurrency::GetHandle ()
{
	if (_phBRLCurrency == NULL)
	{
		_phBRLCurrency = new Handle<QuantLib::BRLCurrency> (*_ppBRLCurrency);
	}
	return *_phBRLCurrency;
}
#endif
bool Cephei::QL::Currencies::CBRLCurrency::HasNative () 
{
	return (_ppBRLCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IBRLCurrency^ Cephei::QL::Currencies::CBRLCurrency_Factory::Create ()
{
    return gcnew CBRLCurrency ();
}
